package com.yong.ssj.control.consumer.sys;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yong.ssj.db.dto.customer.CustomerDto;
import com.yong.ssj.service.sys.IUserService;

@Controller
@RequestMapping("/consumer/sys")
public class UserControllerph {
	@Resource(name="userServiceImpl")
	private IUserService userService;
	
	@RequestMapping("welcome")  
    public String initString(){  //欢迎界面
        return "consumer/sys/welcome";   
    }  
	
	@RequestMapping("login")  
    public String dengluString(){  //登录界面
        return "consumer/sys/login";   
    }  
    
    @RequestMapping("login_do")  
    public String yanzhengString(String customer_name,String customer_password,HttpSession session,ModelMap rps){  //登录验证
    	CustomerDto dto = userService.loginph(customer_name, customer_password);
    	if(dto==null){
    		rps.addAttribute("tishi", "账号不存在!!!");
			return "consumer/sys/login";
    	}else if(dto.getCustomer_password().equals(customer_password)){
			String string=String.valueOf(dto.getCustomer_id());
			session.setAttribute("customer_id", string);
			session.setAttribute("customer_name", customer_name);
			return "forward:/consumer/customer/index";
		}else{
			rps.addAttribute("tishi", "账号或密码错误!!!");
			return "consumer/sys/login";
		}   
    }
    
    @RequestMapping("register")  
    public String zhuceString(){  //注册界面
        return "consumer/sys/register";   
    }  
    
    @RequestMapping("register_do")  
    public String registerString(String customer_name,String customer_password,String yanzheng,HttpSession session,ModelMap rps){  //注册操作
/*		if(yanzheng.equals("")){*/
			int yan = userService.loginphyz(customer_name);
			if(yan==1){
				rps.addAttribute("tishi", "您的手机号码已经注册过请直接登录");
				return "consumer/sys/register";
			}else {
				userService.register(customer_name, customer_password);
				CustomerDto dto = userService.loginph(customer_name, customer_password);//
				session.setAttribute("customer_id", String.valueOf(dto.getCustomer_id()));
				session.setAttribute("customer_name", customer_name);
				rps.addAttribute("tishi", "注册成功，赶快进行身份验证吧");
				return "consumer/customer/select";
			}
		/*}else{
			rps.addAttribute("tishi", "验证码错误!!!");
			return "consumer/sys/register";
		}   */
    }
    
    @RequestMapping("xieyi")  
    public String xieString(){  //协议界面
        return "consumer/sys/xieyi";   
    }  
    
    @RequestMapping("reset")  
    public String resetString(){  //重置密码界面
        return "consumer/sys/reset";   
    }  
    
    @RequestMapping("reset_do")  
    public String resetdo(String customer_name,String customer_password,String yanzheng,ModelMap rps,HttpSession session){  //重置密码操作
    	String customer_id = (String)session.getAttribute("customer_id");
		/*if(yanzheng.equals("")){*/
			int yan = userService.reset(customer_name, customer_password, Long.parseLong(customer_id));
			if(yan==1){
				rps.put("tishi", "密码重置成功！");
			}else {
				rps.put("tishi", "密码重置失败！");
			}
			return "consumer/sys/reset";
		/*}else{
			rps.addAttribute("tishi", "验证码错误!!!");
			return "consumer/sys/reset";//要直接输出流
		}   */
    }
    
    @RequestMapping("mima")  
    public String mimaString(){  //修改密码界面
        return "consumer/sys/mima";   
    }  
    
}






